﻿//@ sourceURL=diningtablefeedetail_hot.js
/**
 * 菜单
 */
; (function (global, factory) {
	if (typeof module === "object" && typeof module.exports === "object") {
		module.exports = factory(global);
	} else {
		factory(global);
	}
})(typeof window !== "undefined" ? window : this, function (window) {
	function addHeaderLabel(LODOP, name, top, left, width, height) {
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left.toFixed(2) + "mm", width + "mm", height + "mm", name);
		//LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
	}
	function addHeaderValue(LODOP, value, top, left, width, height) {
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left.toFixed(2) + "mm", width + "mm", height + "mm", value);
	}
	function addLine(LODOP, top, left, width, height) {
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left + "mm", width + "mm", height + "mm", "ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ");
	}

	function designAndPrintData(LODOP, config) {
		// LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
		var i,j;
		var top = 0;
		var titleHeight = 12;//标题高度
		var hdrHeight = 5;//头部高度
		var lineHeight = 3;//线高度
		var detailHeight = 6;//
		var titleSize = 18;
		var hdrSize = 14;
		var detailSize = 18;
		var printerIndex = -1;
		if (config.Config.PrinterName) {
			for (var i = LODOP.GET_PRINTER_COUNT() - 1; i >= 0; i--) {
				if (LODOP.GET_PRINTER_NAME(i) == config.Config.PrinterName) {
					printerIndex = i;
					break;
				}
			}
		}

		console.log('diningtablefeedetail_hot', new Date().toTimeString().split(' ')[0], printerIndex, config.Config.PrinterName);
		if (printerIndex == -1) {
			console.log('未找到打印机：', config.model);
			if (config.model.ID) {
				resetPrint(config.model.ID);
			}
			return;
		}
		var details = [{ title: config.Config.title + '加菜', details: config.model.added }, { title: '【退】' + config.Config.title + '退菜', details: config.model.deleted }];
		for (i = 0; i < details.length; i++) {
			if (!details[i].details || details[i].details.length == 0) {
				continue;
			}
			for (j = 0; j < details[i].details.length; j++) {
				LODOP.PRINT_INIT("");//必须打印初始化一下
				LODOP.SET_PRINTER_INDEXA(printerIndex);

				top = 15;
				LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", titleHeight + "mm", config.Config.orgname+" "+ config.model.TableName);
				LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
				LODOP.SET_PRINT_STYLEA(0, "FontSize", 14);
				//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
				top += titleHeight;

				LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "70mm", titleHeight + "mm", details[i].title);
				LODOP.SET_PRINT_STYLEA(0, "FontSize", titleSize);
				LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
				LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
				LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
				//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
				top += titleHeight;

				LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", detailHeight + "mm", "品名                单位 数量");
				LODOP.SET_PRINT_STYLEA(0, "FontSize", hdrSize);
				//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
				top += 10;

				//addLine(LODOP, top, 0, config.width, lineHeight);
				//top += lineHeight;

				var a = details[i].details[j].SkuName.split('(');
				if (a[0]) {
					LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "72mm", detailHeight + "mm", a[0]);
					LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
					LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

					LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "51mm", "8mm", detailHeight + "mm", details[i].details[j].UnitMin);
					LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);//1--左靠齐 2--居中 3--右靠齐
					LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
					LODOP.SET_PRINT_STYLEA(0, "Bold", 1);

					LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "12mm", detailHeight + "mm", details[i].details[j].Ordered);
					LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
					LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);//1--左靠齐 2--居中 3--右靠齐
					LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
					LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
				}
				if (a[1]) {
					top += 8
					LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "72mm", detailHeight + "mm", '【注】' + a[1]);
					LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
					LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
				}
				top += 20;
				LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "29mm", "43mm", titleHeight + "mm", config.model.FeeDate);
				LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
				LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
				//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
				
				//LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "64mm", "8mm", detailHeight + "mm", "");//todo:fix
				
				LODOP.NEWPAGE();
				
				LODOP.PRINT();
			}
		}
	}

	function printInit(printTplConfig, tplData, action) {
		if (!printTplConfig.SvrHost) {
			printTplConfig.SvrHost = "http://localhost:8000";
		}
		jQuery.ajax({
			url: printTplConfig.SvrHost + (printTplConfig.SvrHost.substr(-1) === "/" ? "" : "/") + "CLodopfuncs.js?priority=" + new Date().getTime(),
			dataType: "script",
			success: function (resp) {
				setTimeout(function () {
					var LODOP;
					try {
						LODOP = getCLodop();
						if (!LODOP) {
							layer && layer.open({
								type: 1,
								title: false,
								closeBtn: 0,
								shadeClose: true,
								content: "<font color='#FF00FF'>Web打印服务CLodop未安装启动，点击<a href='CLodop_Setup_for_Win32NT.exe' target='_blank'>这里</a>下载安装（若已安装过，可点击<a href='CLodop.protocol:setup' target='_self'>这里</a>直接启动），成功后请刷新本页面。</font>"
							});
							return;
						}
						console.log('LODOP.CVERSION:', LODOP.CVERSION);
						if (LODOP.CVERSION < "3.0.8.3") {
							layer && layer.open({
								type: 1,
								title: false,
								closeBtn: 0,
								shadeClose: true,
								content: "<br><font color='#FF00FF'>Web打印服务CLodop需升级!点击<a href='CLodop_Setup_for_Win32NT.exe' target='_blank'>这里</a>执行升级,升级成功后请刷新本页面。</font>"
							});
						}
						//===如下空白位置适合调用统一功能(如注册语句、语言选择等):==
						LODOP.SET_LICENSES("杭州感知科技有限公司", "89600FD5AB0D66747807D05CE0721A65", "", "");
						//=======================================================
					} catch (err) {
						alert("打印控件初始化失败:" + err);
					}
					if (!tplData || ((!tplData.added || tplData.added.length == 0) && (!tplData.deleted || tplData.deleted.length == 0))) {
						return;
					}
					var printConfig = {};
					printConfig.width = 80;//mm
					printConfig.height = 3.5;//mm
					//注意：得力DL-5801P打印机不支持自定义纸张类型，注释掉下面一行就行
					//LODOP.SET_PRINT_PAGESIZE(3, printConfig.width * 10, printConfig.height * 10, "");////设定打印页面大小:宽度固定、高度自适应
					printConfig.model = tplData;
					printConfig.Config = { orgname: globalData && globalData.user && globalData.user.OrgName || '', title: printTplConfig.Title,PrinterName: printTplConfig.PrinterName, psize: printTplConfig.PageSize, phone: printTplConfig.Phone, address: printTplConfig.DetailAddress, footer: printTplConfig.Footer };
					
					designAndPrintData(LODOP, printConfig);
					LODOP.PRINT(); //打印
					//switch (action) {
					//	case "DESIGN":
					//		LODOP.PRINT_DESIGN(); //打印设计
					//		break;
					//	case "SETUP":
					//		LODOP.PRINT_SETUP(); //打印维护
					//		break;
					//	case "PREVIEW":
					//		LODOP.PREVIEW(); //打印预览
					//		break;
					//	case "PRINT":
					//		LODOP.PRINT(); //打印
					//		break;
					//	default:
					//		{
					//			//LODOP.PRINT(); //直接打印
					//			//LODOP.SEND_PRINT_RAWDATA(String.fromCharCode(27, 112, 0, 60, 255));//打开钱箱指令未测试
					//			//LODOP.SEND_PRINT_RAWDATA(String.fromCharCode(27, 112, 1, 128, 128));//打开钱箱
					//		}
					//		break;
					//}
					// LODOP.SET_PREVIEW_WINDOW(0, 0, 0,1024 , 700, "预览查看.开始打印");
					// LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);//横向时的正向显示
					// LODOP.SET_SHOW_MODE("SETUP_ENABLESS", 11111111000001);
				}, 100);
			},
			error: function (xhr, err, ex) {
				alert("连接打印服务器失败:" + err);
			}
		});
	}

	window.diningtablefeedetail_hot = printInit;
});
